package com.kws.system.service.contractTemplateService.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.system.domain.contractManagementDao.TbContractTemplateFile;
import com.kws.system.mapper.contractTemplateMapper.ContractTemplateFileMapper;
import com.kws.system.service.contractTemplateService.ContractTemplateFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>ContractTemplateFileServiceImpl</b>
 *
 * @description: ContractTemplateFileServiceImpl <br>
 * @date: 2024/11/22 14:36 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class ContractTemplateFileServiceImpl extends ServiceImpl<ContractTemplateFileMapper, TbContractTemplateFile> implements ContractTemplateFileService {
    private static final Logger logger = LoggerFactory.getLogger(ContractTemplateFileServiceImpl.class);

    @Override
    public void createcontractTemplateFile(Long templateId, List<String> fileUrls) {
        if (CollectionUtil.isNotEmpty(fileUrls)){
            List<TbContractTemplateFile> templateFileList = new ArrayList<>();
            fileUrls.forEach(item -> {
                TbContractTemplateFile tbContractTemplateFile = new TbContractTemplateFile();
                tbContractTemplateFile.setTemplateId(templateId);
                tbContractTemplateFile.setFileUrl(item);
                templateFileList.add(tbContractTemplateFile);
            });
            boolean b = this.saveBatch(templateFileList);
        }
    }

    @Override
    public List<String> queryTemplateFileById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("template_id", id);
        List<TbContractTemplateFile> files = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(files)){
            List<String> fileUrlList = files.stream().map(TbContractTemplateFile::getFileUrl).collect(Collectors.toList());
            return fileUrlList;
        }
        return null;
    }
}
